package com.zm.exam.springbootexam.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;

import com.zm.exam.springbootexam.entry.Question;
import com.zm.exam.springbootexam.entry.Result;
import com.zm.exam.springbootexam.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zm
 * @since 2024-08-14
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionMapper questionMapper;

    @RequestMapping("query")
    public Page<Question> query(Question question,
                                @RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10")int size){
        System.out.println("question = " + question.getType());
//        question.setType("填空题");
        LambdaQueryWrapper<Question> lqw = new LambdaQueryWrapper<>();
        if(question.getType()!=null && question.getType().isBlank()==false){
            lqw.eq(Question::getType, question.getType());
        }
        if(question.getCourse()!=null && question.getCourse().isBlank()==false){
            lqw.eq(Question::getCourse, question.getCourse());
        }
        if(question.getContent()!=null && question.getContent().isBlank()==false){
            lqw.like(Question::getContent, question.getContent());
        }
        Page<Question> p = new PageDTO<>(page, size);
        return questionMapper.selectPage(p, lqw);
    }

    @Transactional
    @RequestMapping("delete")
    public Result delete(@RequestBody Question question){
        System.out.println("question = " + question);
        LambdaQueryWrapper<Question> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Question::getId,question.getId());
        int result = questionMapper.delete(lqw);
        if(result>0){
            return new Result(1,"删除成功",null);
        }else {
            return new Result(0,"删除失败",null);
        }

    }

    @Transactional
    @RequestMapping("update")
    public Result update(@RequestBody Question question){
        System.out.println("question = " + question);
        LambdaQueryWrapper<Question> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Question::getId,question.getId());
        int result =questionMapper.update(question,lqw);
        if(result>0){
            return new Result(1,"修改成功",null);
        }else {
            return new Result(0,"修改失败",null);
        }
    }

    @Transactional
    @RequestMapping("add")
    public Result add(@RequestBody Question question){
        System.out.println("question = " + question);
        int result = questionMapper.insert(question);
        if(result>0){
            return new Result(1,"添加成功",null);
        }else {
            return new Result(0,"添加失败",null);
        }

    }
}

